import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ShapeDrawer extends JFrame {
    private DrawArea drawArea = new DrawArea();

    public ShapeDrawer() {
        this.setSize(400, 400);
        this.setDefaultCloseOperation(3);
        this.setTitle(" 图形绘制器 ");
        this.add(this.drawArea, "Center");
        JComboBox<String> shapeSelector = new JComboBox(new String[]{" 正⽅形 ", "⻓⽅形 ", " 圆形 ", " 三⻆形 "});
        this.add(shapeSelector, "North");
        shapeSelector.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox cb = (JComboBox)e.getSource();
                String shapeName = (String)cb.getSelectedItem();
                int size = 0;
                boolean validInput = false;
                String input = JOptionPane.showInputDialog(" 请输⼊ " + shapeName + " 的尺⼨:");

                try {
                    size = Integer.parseInt(input);
                    validInput = true;
                } catch (NumberFormatException var8) {
                    JOptionPane.showMessageDialog((Component)null, "输入无效，请输入一个整数");
                }

                ShapeDrawer.this.drawArea.setShape(shapeName, size);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                (new ShapeDrawer()).setVisible(true);
            }
        });
    }

    private static class DrawArea extends JPanel {
        private String shape = "正⽅形";
        private int size = 100;

        private DrawArea() {
        }

        public void setShape(String shape, int size) {
            this.shape = shape;
            this.size = size;
            this.repaint();
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            switch (this.shape) {
                case " 正⽅形 ":
                    g.drawRect(50, 50, this.size, this.size);
                    break;
                case "⻓⽅形 ":
                    g.drawRect(50, 50, this.size * 2, this.size);
                    break;
                case " 圆形 ":
                    g.drawOval(50, 50, this.size, this.size);
                    break;
                case " 三⻆形 ":
                    int[] xPoints = new int[]{50 + this.size / 2, 50, 50 + this.size};
                    int[] yPoints = new int[]{50, 50 + this.size, 50 + this.size};
                    g.drawPolygon(xPoints, yPoints, 3);
            }

        }
    }
}